
#import "PickViewController.h"

@interface PickViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *pickView;
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (nonatomic, strong) NSMutableArray * selectIndexArray;
@property (nonatomic, strong) NSMutableArray *year;
@property (nonatomic, strong) NSMutableArray *month;
@property (nonatomic, strong) NSMutableArray *day;

@end

@implementation PickViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    CGRect bounds = CGRectMake(0, 0, SCREEN_WIDTH, self.bgView.frame.size.height);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = bounds;
    layer.path = path.CGPath;
    self.bgView.layer.mask = layer;
    self.bgView.bgHexColor = @"#FFFFFF";
}
- (IBAction)actionForBtn:(UIButton *)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
    if (sender.tag == 202) {
        if (self.stampBlock) {
            WS(self)
            NSInteger stamp = 0;
            NSInteger index1 = [weakself.selectIndexArray[0] intValue];
            NSInteger index2 = [weakself.selectIndexArray[1] intValue];
            NSString *time = [NSString stringWithFormat:@"%@-%@",weakself.year[index1],weakself.month[index2]];
            NSString *formatter = @"yyyy-MM";
            if (weakself.type == DateTypeday) {
                NSInteger temp = [[NSDate currentTimeStamp:@"yyyy-MM-dd HH:mm:ss"] intValue];
                NSString *hour = [NSDate transToDate:@"HH:mm:ss" :temp];
                NSInteger index3 = [weakself.selectIndexArray[2] intValue];
                time = [NSString stringWithFormat:@"%@-%@ %@",time,weakself.day[index3],hour];
                formatter = @"yyyy-MM-dd HH:mm:ss";
            }
            stamp = [[NSDate tranToStamp:time :formatter] intValue];
            weakself.stampBlock(stamp);
        }else{
            if (self.block) {
                self.block(self.selectIndexArray);
            }
        }
    }
}
- (void)setArray:(NSArray *)array{
    _array = array;
    self.selectIndexArray = [NSMutableArray array];
    for (int i = 0; i < array.count; i++) {
        [self.selectIndexArray addObject:@(0)];
    }
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:0 inComponent:0 animated:NO];
}
- (void)setType:(DateType)type{
    _type = type;
    [self setupdata];
}
- (void)setStamp:(NSInteger)stamp{
    _stamp = stamp;
    [self setupdata];
}
- (void)setupdata{
    if (self.stamp == 0) {
        return;
    }
    self.year = [NSMutableArray array];
    self.month = [NSMutableArray array];
    self.day = [NSMutableArray array];
    NSDateComponents *date = [NSDate dateOfStamp:self.stamp];
    
    for (int i = (int)date.year; i > date.year - 30; i--) {
        [self.year addObject:[NSString stringWithFormat:@"%d",i]];
    }
    for (int i = 1; i < 13; i++) {
        [self.month addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    if (self.type == DateTypeday) {
        [self getday:self.stamp];
    }
    NSArray *array = self.type == DateTypeday ? @[self.year,self.month, self.day] : @[self.year,self.month];
    self.array = array;
}
- (void)getday:(NSInteger)stamp{
    NSInteger day = [NSDate numOfMonth:stamp];
    [self.day removeAllObjects];
    for (int i = 1; i <= day; i++) {
        [self.day addObject:[NSString stringWithFormat:@"%02d",i]];
    }
}
- (void)show{
    [[self currentVC] presentViewController:self animated:NO completion:nil];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.array.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.array[component] count];
}
#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 100;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.selectIndexArray[component] = @(row);
    if (self.type == DateTypeday && component != 2) {
        NSInteger index = [self.selectIndexArray[0] intValue];
        NSInteger index2 = [self.selectIndexArray[1] intValue];
        NSString *time = [NSString stringWithFormat:@"%@-%@-01",self.year[index],self.month[index2]];
        NSInteger stamp = [[NSDate tranToStamp:time :@"yyyy-MM-dd"] intValue];
        [self getday:stamp];
        [pickerView reloadComponent:2];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lbl = [UILabel new];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.textHexColor = @"#1A1A1A";
    lbl.font = [UIFont systemFontOfSize:16];
    lbl.text = LS(self.array[component][row]);
    return lbl;
}

@end
